/**
 * 
 */
package com.yerba.ricecooker.chart;

import com.yerba.ricecooker.utility.CommonHelper;

/**
 * @author mxie
 * 
 */
public class AnnotatedTimeLineChart
{
    public AnnotatedTimeLineChart(AnnotatedTimeLineDataSet dataset)
    {
        setDataset(dataset);
    }

    AnnotatedTimeLineDataSet dataset = null;

    public AnnotatedTimeLineDataSet getDataset()
    {
        return dataset;
    }

    public void setDataset(AnnotatedTimeLineDataSet dataset)
    {
        this.dataset = dataset;
    }

    public void createChartPage()
    {
        setChartHtmlPage("");

        addHtmlLine("<html>");
        addHtmlLine("<head>");

        addHtmlLine("<script type='text/javascript' src='https://www.google.com/jsapi'></script>");
        addHtmlLine("<script type='text/javascript'>");
        addHtmlLine("google.load('visualization', '1', {'packages':['annotatedtimeline']});");

        addHtmlLine("google.setOnLoadCallback(drawChart);");
        addHtmlLine("function drawChart() {");
        addHtmlLine("var data = new google.visualization.DataTable();");
        addHtmlLine("data.addColumn('date', 'Date');");

        // set labels
        for (int i = 0; i < getDataset().getNumberOfLines(); i++)
        {
            addHtmlLine("data.addColumn('number', '" + getDataset().getLabels().get(i).trim() + "');");
        }

        addHtmlLine("data.addRows([");
        
        // set data
        for (int i = 0; i < getDataset().size(); i++)
        {
            AnnotatedTimeLineDataRow row = getDataset().get(i);
            String dataline = "[new Date(" + row.getDateYear() + ", " + row.getDateMonth() + ", " + row.getDateDay() + "), ";
            for (int j = 0; j < row.getNumberOfUnits(); j++)
            {
                double unitdata = row.getDataRow(j);
                dataline += CommonHelper.formatDouble(unitdata);
                
                if (j + 1 < row.getNumberOfUnits())
                    dataline += ", ";
            }
            dataline += "]";
            if (i + 1 < getDataset().size())
                dataline += ",";
            
            addHtmlLine(dataline);
        }
        
        addHtmlLine("]);");

        addHtmlLine("var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));");
        addHtmlLine("chart.draw(data, {displayAnnotations: true, scaleType: 'maximized', numberFormats: '#.###', thickness: 1});");
        addHtmlLine("}");
        addHtmlLine("</script>");
        addHtmlLine("</head>");

        addHtmlLine("<body>");
        addHtmlLine("<br>");
        addHtmlLine("<div id='chart_div' style='width: 1520px; height: 855px;'></div>");
        addHtmlLine("</body>");
        addHtmlLine("</html>");
    }

    private String chartHtmlPage = "<html><body>-Empty-Chart-Page-</body><html>";

    public String getChartHtmlPage()
    {
        return chartHtmlPage;
    }

    public void setChartHtmlPage(String chartHtmlPage)
    {
        this.chartHtmlPage = chartHtmlPage;
    }

    private void addHtmlLine(String line)
    {
        this.chartHtmlPage += line + CommonHelper.NEWLINE;
    }

}
